package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.api.ApiService;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxObserver;
import com.shqcjd.yunweiyanoperator.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.yunweiyanoperator.model.ResponseData;
import com.shqcjd.yunweiyanoperator.model.eventbus.CompanyRegisterSuccessEvebt;
import com.shqcjd.yunweiyanoperator.model.eventbus.FinishActivityEvent;
import com.shqcjd.yunweiyanoperator.model.pojo.RegisterCompanyBean;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CompanyRegisterActivity extends BaseActivity {

    @BindView(R.id.tv_register_company_next)
    TextView tvRegisterCompanyNext;
    @BindView(R.id.et_register_company)
    EditText etRegisterCompany;
    @BindView(R.id.et_register_company_business)
    EditText etRegisterCompanyBusiness;
    @BindView(R.id.et_register_company_tel)
    EditText etRegisterCompanyTel;
    @BindView(R.id.tl_register_company)
    TextInputLayout tlRegisterCompany;
    @BindView(R.id.tl_register_company_business)
    TextInputLayout tlRegisterCompanyBusiness;
    @BindView(R.id.tl_register_company_tel)
    TextInputLayout tlRegisterCompanyTel;
    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private Context mContext;


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_company_register;
    }

    @Override
    public void init() {
        super.init();
        mContext = this;
    }

    @Override
    public void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        tvTitle.setText("公司注册");
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        addListener(tlRegisterCompany, etRegisterCompany, "企业名称不能为空");
        addListener(tlRegisterCompanyBusiness, etRegisterCompanyBusiness, "主营业务不能为空");
    }

    @OnClick({R.id.tv_register_company_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register_company_next:
                if(!isNull()){
                                    RegisterCompanyBean mBean = new RegisterCompanyBean();
                                    mBean.businessLicenseUrl = "";
                                    mBean.businessName = etRegisterCompany.getText().toString().trim();
                                    mBean.mainBusiness = etRegisterCompanyBusiness.getText().toString().trim();
                                    mBean.telNumber = etRegisterCompanyTel.getText().toString().trim();
                                    Intent it = new Intent(mContext, LicenseUpActivity.class);
                                    it.putExtra("mBean", mBean);
                                    startActivity(it);
                }else{
                    ToastUtils.showShort(mContext, "请确认所有信息填写完毕");
                }
                break;
        }
    }



    private boolean isNull(){
        if(!etRegisterCompany.getText().toString().equals("")&&
                !etRegisterCompanyBusiness.getText().toString().equals("")){
            return false;
        }
        return true;
    }

    private void addListener(TextInputLayout textInputLayout, EditText editText, String str){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                notNull(textInputLayout, editText, str);
            }
        });
    }

    private boolean notNull(TextInputLayout textInputLayout, EditText editText, String str) {
        if (TextUtils.isEmpty(editText.getText().toString().trim())) {
            textInputLayout.setError(str);
            return false;
        } else {
            textInputLayout.setError(null);
            textInputLayout.setErrorEnabled(false);

        }
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishCompanyRegisterEvent(CompanyRegisterSuccessEvebt event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
